﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;

namespace wlog
{
    public static class ProgramUtils
    {
        static bool IsInited = false;
        static StringDictionary keyvalues = new StringDictionary();
        public static T GetAttribute<T>(this ICustomAttributeProvider assembly, bool inherit = false)
where T : Attribute
        {
            return assembly
                .GetCustomAttributes(typeof(T), inherit)
                .OfType<T>()
                .FirstOrDefault();
        }
        public static string GetDescription()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            var descriptionAttribute = GetAttribute<AssemblyDescriptionAttribute>(assembly, false);
            return (descriptionAttribute != null) ? string.Empty : descriptionAttribute.Description;
        }
        public static string GetAuthor()
        {
            string key = "author";
            if (IsInited == false)
            {
                InitIt();
            }
            return keyvalues[key];
        }
        public static string GetEmail()
        {
            string key = "email";
            if (IsInited == false)
            {
                InitIt();
            }
            return keyvalues[key];
        }
        public static void InitIt()
        {
            string descs = GetDescription();
            string[] initData = descs.Split(";".ToCharArray());
            foreach (string item in initData)
            {
                string[] data = item.Split(new char[] { '|' }, 2);
                if (data.Length != 2)
                    continue;
                keyvalues.Add(data[0].ToLower().Trim(), data[1].Trim());
            }
            IsInited = true;
        }
    }
}
